package com.jg.visitor.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.jg.visitor.common.bean.UploadUserBean;
import com.jg.visitor.common.bean.UploadUserBeanInfo;
import com.jg.visitor.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

@Slf4j
public class BatchUploadUserExcelListener implements ReadListener<UploadUserBean> {
    private final UserService userService;

    public BatchUploadUserExcelListener(UserService userService){
        this.userService = userService;
    }

    @Override
    public void invoke(UploadUserBean data, AnalysisContext analysisContext) {
        int sheetNo = analysisContext.readSheetHolder().getSheetNo() + 1;
        String sheetName = analysisContext.readSheetHolder().getSheetName();

        if(sheetNo >= 1 && sheetNo <= 3){
            UploadUserBeanInfo uploadUserBeanInfo = new UploadUserBeanInfo();
            BeanUtils.copyProperties(data,uploadUserBeanInfo);
            uploadUserBeanInfo.setSheet(analysisContext.readSheetHolder().getSheetName());
            uploadUserBeanInfo.setSheetNo(analysisContext.readSheetHolder().getSheetNo() + 1);
            uploadUserBeanInfo.setRow(analysisContext.readSheetHolder().getRowIndex());
            userService.addSingleRequestData(uploadUserBeanInfo);
            return;
        }

        log.error("sheet not support,sheetName = {},sheetNo = {}",sheetName,sheetNo);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
